home *** CD-ROM | disk | FTP | other *** search
- Path: newsserver.trl.OZ.AU!cypress!lawsonh
- From: lawsonh@melb.cpr.itg.telecom.com.au (Lawson Hanson)
- Newsgroups: comp.lang.c
- Subject: Re: Hiding a password
- Date: 12 Mar 96 20:59:00 GMT
- Organization: Telecom Research Laboratories, Melbourne, Australia.
- Message-ID: <lawsonh.826664340@cypress>
- References: <1996Feb29.224936.137160@forest>
- NNTP-Posting-Host: 144.136.133.19
- X-Newsreader: NN version 6.5.0 #2 (NOV)
-
- ebromber@forest.drew.edu writes:
-
- >I recently wrote a password program. However, there is one little flaw I
- >want to fix. When the password is entered, it is visible on the screen. I
- >was wondering if anyone knows how to hide the password by printing
- >asterisks instead of the actual character.
- >Thanks in advance.
-
- >ebromber@drew.edu
-
- Here is an example (it is awful, but works on Unix).
- It does not echo "asterisks", just stays blank,
- then prints out the password it read:
- ------------------------------------
- /*
- ** Program : getpass.c
- ** Purpose : Turn off echoing to get a `secret' password
- ** : for use by shell programs, etc.
- ** Usage : PASS=`getpass "Enter password"`
- ** : . . . ${PASS} . . . # do whatever
- ** : PASS="" # null out the PASS variable
- */
- #include <signal.h>
- #include <stdio.h>
- #include <termios.h>
-
-
- extern char *getpass(); /* calls standard `getpass(3)' function */
-
-
- int main( argc, argv )
- int argc;
- char *argv[];
- {
- char *ptr;
- char str[BUFSIZ];
-
- str[0] = '\0';
-
- while ( --argc > 0 )
- {
- strcat( str, *++argv );
- strcat( str, " " );
- }
-
- if ( strlen( str ) == 0 )
- strcat( str, "Enter Password: " );
-
- if ( ( ptr = getpass( str ) ) == NULL )
- {
- fprintf( stderr, "getpass: ERROR: returned NULL\n" );
- exit( 1 );
- }
-
- /* print the entered password
- */
- printf( "%s\n", ptr );
-
- /* now we could use the password . . .
- */
-
- /* zero it out when we're done with it
- */
- while ( *ptr != 0 )
- *ptr++ = 0;
-
- exit( 0 );
- }
- ------------------------------------
-
- Best regards,
-
- Lawson Hanson
-
-